﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Demo.Hosting.SelfHosting
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(Demo.Hosting.IISHosting.CalculatorService), new Uri("http://localhost:8000/CalculatorService/")))
            {
                // 检查配置文件中是否配置过behavior
                if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
                {
                    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                    behavior.HttpGetEnabled = true;

                    //HttpGetUrl属性用来获取元数据，如果不设置改属性只能通过基地址+wsdl的方式获取
                    behavior.HttpGetUrl = new Uri("http://localhost:8000/CalculatorService/metadata");
                    host.Description.Behaviors.Add(behavior);
                }

                //Add Endpoint
                host.AddServiceEndpoint(typeof(Demo.Hosting.IISHosting.ICalculator), new WSHttpBinding(), "");
                // Setting Timeout 10
                host.CloseTimeout = TimeSpan.FromSeconds(10);

                host.Open();
                Console.WriteLine("Service has been started");
                Console.Read();
                host.Close();

            }
        }
    }
}
